Skip to content

Conversation

@RogerHYang
Copy link
Contributor

@RogerHYang RogerHYang commented Nov 6, 2025

resolves #2381


Note

Improve token usage extraction to handle Anthropic cache read/write and LangChain UsageMetadata (with Bedrock heuristics), update deps, and add targeted tests.

  • Instrumentation (_tracer.py):
    • Token count parsing:
      • Add _is_lc_usage_metadata/_token_counts_from_lc_usage_metadata to map langchain_core.messages.ai.UsageMetadata, including audio/reasoning and cache details with Bedrock-specific heuristics.
      • Add _is_raw_anthropic_usage_with_cache_read_or_write/_token_counts_from_raw_anthropic_usage_with_cache_read_or_write to handle Anthropic cache_read_input_tokens/cache_creation_input_tokens and emit detailed cache attributes.
      • Integrate both into _token_counts; remove previous key-based Anthropic handling.
    • Minor: type additions (TypedDict, TypeGuard, UsageMetadata).
  • Tests:
    • New tests/test_token_counts.py covering LC usage metadata and Anthropic cache scenarios.
    • Update tests/test_instrumentor.py Anthropic expectations (LLM_TOKEN_COUNT_PROMPT from 22 to 33).
  • Dependencies:
    • Bump langchain_core to >= 0.3.9 (instruments) and == 0.3.9 (type-check).
  • CI/Tooling:
    • tox.ini: add uv pip list -v in commands_pre.

Written by Cursor Bugbot for commit 30a6e95. This will update automatically on new commits. Configure here.

@RogerHYang RogerHYang requested a review from a team as a code owner November 6, 2025 16:57
@dosubot dosubot bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Nov 6, 2025
@RogerHYang RogerHYang marked this pull request as draft November 6, 2025 21:38
@RogerHYang RogerHYang marked this pull request as ready for review November 7, 2025 17:18
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:XL This PR changes 500-999 lines, ignoring generated files. labels Nov 7, 2025
"cache_read_input_tokens" in obj
and isinstance(obj["cache_read_input_tokens"], int)
or "cache_creation_input_tokens" in obj
and isinstance(obj["cache_creation_input_tokens"], int)
Copy link
Collaborator

@caroger caroger Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Add explicit parentheses in the type guard for clarity

return (
    "input_tokens" in obj
    and "output_tokens" in obj
    and isinstance(obj["input_tokens"], int)
    and isinstance(obj["output_tokens"], int)
    and (
        ("cache_read_input_tokens" in obj and isinstance(obj["cache_read_input_tokens"], int))
        or ("cache_creation_input_tokens" in obj and isinstance(obj["cache_creation_input_tokens"], int))
    )
)

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Nov 10, 2025
)
):
return
keys: Sequence[str]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Anthropic Usage Metrics Disappear

Removing input_tokens and output_tokens from the generic token count extraction breaks basic Anthropic responses that don't include cache tokens or total_tokens. Previously, responses with just input_tokens and output_tokens (standard Anthropic format without caching) would be captured by the first loop. Now they're only handled by _is_raw_anthropic_usage_with_cache_read_or_write, which requires cache tokens to be present, causing basic Anthropic token counts to be lost.

Fix in Cursor Fix in Web

@RogerHYang RogerHYang merged commit 9f21f0f into main Nov 10, 2025
13 checks passed
@RogerHYang RogerHYang deleted the fix-anthropic-cache-token-count branch November 10, 2025 21:18
@github-actions github-actions bot mentioned this pull request Nov 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG]: Prompt Caching Price Miscalculation

3 participants